<template>
  <!-- 添加或修改商家促销活动对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="全平台折扣" prop="platformDiscount">
        <el-input v-model="form.platformDiscount" placeholder="请输入全平台折扣"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {saveGoldCoinConfig, queryGoldDetail} from "@/api/autoOperation/fullReduction"

export default {
  name: "FullReduction",
  props: {
    storeId: {
      type: String,
      default: ''
    },
    configId: { //配置id
      type: String,
      default: ""
    },
    userId: { // 绑定店铺的用户id
      type: String,
      default: ""
    },
    isUseShareCode: { // 是否使用分享码
      type: String,
      default: "0"
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 弹出层标题
      title: "新增金币折扣",
      // 是否显示弹出层
      open: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {platformDiscount: [{required: true, message: "全平台折扣不能为空", trigger: "blur"}]},
    };
  },
  created() {
    if (this.configId) {
      this.getConfigDetail()
    } else {
      this.reset()
    }
  },
  methods: {
    // 获取满减配置详情
    getConfigDetail() {
      this.title = "修改金币折扣配置";
      queryGoldDetail({id: this.configId}).then(res => {
        let data = res.data
        for (let key in data) { //将所有字符转成字符串
          if (data[key] != null) {
            data[key] = '' + data[key]
          }
        }
        this.form = data
      })
    },
    // 取消
    cancel() {
      this.$emit('close', false)
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        storeId: this.storeId,
        platformDiscount: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.isUseShareCode == '1') {
            this.form.id = undefined;
            this.form.storeId = this.storeId
            this.form.userId = this.userId
            this.form.storeUserName = this.storeUserName
          }
          saveGoldCoinConfig(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.cancel()
            this.buttonLoading = false;
          });

        }
      });
    },
  }
}
;
</script>
